﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using StructureMap;
using BonsaiScrum.Bus;

namespace BonsaiScrum.Mvc
{
    public class UnitOfWorkModule : IHttpModule
    {
        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.EndRequest += CommitUnitOfWork;
        }

        private static void CommitUnitOfWork(object sender, EventArgs e)
        {
            var commandBus = ObjectFactory.GetInstance<ICommandBus>();

            try
            {
                commandBus.Commit();
            }
            catch
            {
                commandBus.Rollback();
                throw;
            }
        }
    }
}
